------------------------------------
-- Chat suggestions.
------------------------------------
local ChatSuggestions  = {}
ChatSuggestions["a"] = "[text] - Allies chat"
ChatSuggestions["pm"] = "<player> [text]"
ChatSuggestions["drop"] = ""
ChatSuggestions["dropall"] = "- Drops all your weapons."
ChatSuggestions["scan"] = "<player>"
ChatSuggestions["placehit"] = "<player> <ammount>"
ChatSuggestions["gangster"] = " - Become Gangster"
ChatSuggestions["mobboss"] = " - Become MobBoss"
ChatSuggestions["help"] = " - Get help"
ChatSuggestions["write"] = "[text]"
ChatSuggestions["buysign"] = "[text]"
ChatSuggestions["sign"] = "[text]"
ChatSuggestions["type"] = "[text]"
ChatSuggestions["job"] = "[text]"
ChatSuggestions["give"] = "[ammount]"
ChatSuggestions["moneydrop"] = "[ammount]"
ChatSuggestions["mayor"] = "- Become Mayo"
ChatSuggestions["citizen"] = "- Become Citizen"
ChatSuggestions["cop"] = "- Become Cop"
ChatSuggestions["hitman"] = "- Become Hitman"
ChatSuggestions["overwatch"] = "- Become Overwatch"
ChatSuggestions["specops"] = "- Become Specops"
ChatSuggestions["buydrug"] = "[drug name]"
ChatSuggestions["buydrugs"] = "[drug name]"
ChatSuggestions["buypistol"] = "[pistol name]"
ChatSuggestions["buyshipment"] = "[shipment name]"
ChatSuggestions["buyammo"] = "[ammo type]"
ChatSuggestions["upgrade"] = "- Upgrade entity you are looking at"
ChatSuggestions["buyrefinery"] = ""
ChatSuggestions["dropbackpack"] = "- Drop all items in your backpack"
ChatSuggestions["buydrugrefinery"] = ""
ChatSuggestions["buygunfactory"] = ""
ChatSuggestions["buyradar"] = ""
ChatSuggestions["buysupplycabinet"] = ""
ChatSuggestions["buypowerplant"] = ""
ChatSuggestions["buygenerator"] = ""
ChatSuggestions["buybomb"] = ""
ChatSuggestions["buydartgun"] = ""
ChatSuggestions["buypipebomb"] = ""
ChatSuggestions["buyknife"] = ""
ChatSuggestions["buylockpick"] = ""
ChatSuggestions["buybinoculars"] = ""
ChatSuggestions["buywelder"] = ""
ChatSuggestions["buyturret"] = ""
ChatSuggestions["buypipe"] = ""
ChatSuggestions["buydispenser"] = ""
ChatSuggestions["buyhealthdispenser"] = ""
ChatSuggestions["buyspotlight"] = ""
ChatSuggestions["buylamp"] = ""
ChatSuggestions["buymethlab"] = ""
ChatSuggestions["buyweedplant"] = ""
ChatSuggestions["buyspawnpoint"] = ""
ChatSuggestions["buyjeep"] = ""
ChatSuggestions["buyairboat"] = ""
ChatSuggestions["buyarmor"] = ""
ChatSuggestions["buyhelmet"] = ""
ChatSuggestions["buytoolkit"] = ""
ChatSuggestions["buyscanner"] = ""
ChatSuggestions["buyshield"] = ""
ChatSuggestions["buyprinter"] = ""
ChatSuggestions["buymoneyprinter"] = ""
ChatSuggestions["buystill"] = ""
ChatSuggestions["buydruglab"] = ""
ChatSuggestions["buymicrowave"] = ""
ChatSuggestions["buygunlab"] = ""
ChatSuggestions["buygunvault"] = ""
ChatSuggestions["buygunrecycler"] = ""
ChatSuggestions["buymoneyvault"] = ""
ChatSuggestions["buypillbox"] = ""
ChatSuggestions["buyhealth"] = ""
ChatSuggestions["buyticket"] = "[number]"
ChatSuggestions["lotto"] = "- Show lotto stats"

local Suggestions = {}
local Chat

------------------------------------
-- I borrowed this code from
-- Overv's admin mod Evolve,
-- its not one bit mine.
------------------------------------
local function HintsHUDPaint()
	if (Chat && LocalPlayer():GetInfoNum("rpdm_autocomplete") == 1) then
	
		local x, y = chat.GetChatBoxPos()
		x = x + ScrW() * 0.03875
		y = y + ScrH() / 4 + 5
		
		surface.SetFont("ChatFont")
		
		for _, v in ipairs(Suggestions) do			
			draw.SimpleText(v.ChatCommand, "ChatFont", x, y, Color(0, 0, 0, 255))
			draw.SimpleText(" " .. v.Usage or "", "ChatFont", x + surface.GetTextSize(v.ChatCommand), y, Color(0, 0, 0, 255))
			draw.SimpleText(v.ChatCommand, "ChatFont", x, y, Color(0, 255, 0, 255))
			draw.SimpleText(" " .. v.Usage or "", "ChatFont", x + surface.GetTextSize(v.ChatCommand), y, Color(255, 255, 255, 255))
			
			y = y + 17
		end
	end
end
hook.Add("HUDPaint", "HintsHUDPaint", HintsHUDPaint)

local function HintsChatTextChanged(str)
	if (string.Left(str, 1) == "/") then
		local com = string.sub(str, 2, (string.find(str, " ") or (#str + 1)) - 1)
		Suggestions = {}
		for k,v in pairs(ChatSuggestions) do
			if (k and string.sub(k, 0, #com) == string.lower(com) and #Suggestions < 4) then 
			table.insert(Suggestions, { ChatCommand = string.sub(str, 1, 1) .. k, Usage = v or "" }) 
			end
		end
		table.SortByMember(Suggestions, "ChatCommand", function(a, b) return a < b end)
	else
		Suggestions = {}
	end
end
hook.Add("ChatTextChanged", "HintsChatTextChanged", HintsChatTextChanged)

function HintsOnChatTab(str)
	if (string.match(str, "^[/!][^ ]*$") and #Suggestions > 0) then
		return Suggestions[1].ChatCommand .. " "
	end
end
hook.Add("OnChatTab", "HintsOnChatTab", HintsOnChatTab)
------------------------------------
-- Incomming allies msg.
------------------------------------
local function AliesChat(msg)
	str = msg:ReadString()
	ply = msg:ReadEntity()
	local tab = {}
 	
	table.insert(tab, Color(30, 160, 40))
	table.insert(tab, "(TEAM) ")
	table.insert(tab, Color(255, 255, 255))
	table.insert(tab, ply)
	table.insert(tab, ": "..str)
	
	chat.AddText(unpack(tab))
 
	return true
end
usermessage.Hook("allies_chat", AliesChat)

------------------------------------
-- Incomming private chat.
------------------------------------
local function PrivateChat(msg)
	str = msg:ReadString()
	ply = msg:ReadEntity()
	local tab = {}
 	
	table.insert(tab, Color(135, 0, 0))
	table.insert(tab, "(PM) ")
	table.insert(tab, Color(255, 255, 255))
	table.insert(tab, ply)
	table.insert(tab, ": "..str)
	
	chat.AddText(unpack(tab))
 
	return true
end
usermessage.Hook("private_chat", PrivateChat)